package net.wanho.shop.service.impl;

import net.wanho.shop.http.SearchHttpSN;
import net.wanho.shop.service.WeatherService;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 这个类是：
 *
 * @author: MENG HU WANG
 * @date: 2025/1/6 10:20
 * @version: 1.0
 */

@Service
public class WeatherServiceImpl implements WeatherService {
    SearchHttpSN snCal = new SearchHttpSN();

    public static String AK = "dyjoHXemLhSTicubgvTDkjSlNT1kiHIh";

    public static String SK = "HfjmYR2mxCVAQ6inXfP0i30nBVdLDakm";

    public static String URL = "https://api.map.baidu.com/weather/v1/?";

    @Override
    public String getWeather(String cityName) {

        Map<String, String> params = new LinkedHashMap<>();
        params.put("district_id", "320102");
        params.put("data_type", "now");
        params.put("ak", AK);

        try {
            params.put("sn", snCal.caculateSn());
            String s = snCal.requestGetSN(URL, params);

            String paramsStr = snCal.toQueryString(params);

            String wholeStr = "/weather/v1/?" + paramsStr + SK;

            System.out.println(wholeStr);
            String tempStr = URLEncoder.encode(wholeStr, StandardCharsets.UTF_8);

            String sn = snCal.MD5(tempStr);

            System.out.println(sn);
            return sn;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
